package com.warm.common.image;

import org.jfree.chart.*;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.renderer.category.CategoryItemRenderer;
import org.jfree.chart.renderer.category.CategoryStepRenderer;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.chart.title.LegendTitle;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.ui.RectangleEdge;
import org.jfree.ui.RectangleInsets;

import java.awt.*;
import java.io.File;
import java.io.IOException;

/**
 * @Description 测试画图
 * @Author iyb-liangchuang
 * @Date 2021/7/8 17:56
 */
public class JFreeChartUtil {

    private static String titleFont = "微软雅黑";
    private static int titleFontSize = 20;
    private static String xyFont = "微软雅黑";
    private static int xyFontSize = 15;

    public static void main(String[] args) throws IOException {
        //pie();
        //ring();
        bar();
    }

    private static void pie() throws IOException {
        DefaultPieDataset dataset = new DefaultPieDataset();
        dataset.setValue("A", 20d);
        dataset.setValue("B", 10d);
        dataset.setValue("C", 20d);
        dataset.setValue("D", 30d);
        dataset.setValue("F", 20d);

        JFreeChart chart = ChartFactory.createPieChart(
                "Title",
                dataset,
                true, true, false
        );

        File file = new File("D:\\" + System.currentTimeMillis() + ".png");
        ChartUtilities.saveChartAsPNG(file, chart, 650, 500);
    }

    private static void ring() throws IOException {
        DefaultPieDataset dataset = new DefaultPieDataset();
        dataset.setValue("A", 20d);
        dataset.setValue("B", 10d);
        dataset.setValue("C", 20d);
        dataset.setValue("D", 30d);
        dataset.setValue("F", 20d);

        JFreeChart chart = ChartFactory.createRingChart(
                "Title",
                dataset,
                true, true, false
        );


        File file = new File("D:\\" + System.currentTimeMillis() + ".png");
        ChartUtilities.saveChartAsPNG(file, chart, 650, 500);
    }

    private static void bar() throws IOException {
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        dataset.addValue(1.0, "A", "1月");
        dataset.addValue(3.0, "A", "2月");
        dataset.addValue(5.0, "A", "3月");
        dataset.addValue(4.0, "A", "4月");
        dataset.addValue(7.0, "A", "5月");

        JFreeChart chart = ChartFactory.createBarChart(
                "Title",
                "month", "score",
                dataset,
                PlotOrientation.VERTICAL,
                true, true, false
        );

        chart.setBackgroundPaint(ChartColor.WHITE);

        //标题
        TextTitle textTitle = chart.getTitle();
        textTitle.setFont(new Font(titleFont, Font.BOLD, titleFontSize));
        LegendTitle legendTitle = chart.getLegend();
        legendTitle.setItemFont(new Font(titleFont, Font.PLAIN, titleFontSize));

        CategoryPlot plot = chart.getCategoryPlot();
        plot.setBackgroundPaint(ChartColor.darkGray);
        plot.setBackgroundPaint(ChartColor.white);

        //图表xy轴字体设置
        plot.getDomainAxis().setLabelFont(new Font(xyFont, Font.PLAIN, xyFontSize));
        plot.getDomainAxis().setTickLabelFont(new Font(xyFont, Font.PLAIN, xyFontSize));
        plot.getRangeAxis().setTickLabelFont(new Font(xyFont, Font.PLAIN, xyFontSize));
        plot.getRangeAxis().setLabelFont(new Font(xyFont, Font.PLAIN, xyFontSize));

        //设置背景色-xy轴格子色
        plot.setBackgroundPaint(ChartColor.WHITE);
        plot.setRangeGridlinePaint(ChartColor.lightGray);

        BarRenderer renderer = new BarRenderer();
        renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
        renderer.setBaseItemLabelsVisible(true);
        renderer.setItemLabelAnchorOffset(10d);
        renderer.setItemMargin(0.4);
        renderer.setSeriesPaint(0, ChartColor.darkGray);
        renderer.setMaximumBarWidth(0.015);

        plot.setRenderer(renderer);

        chart.getLegend().setPosition(RectangleEdge.RIGHT);

        File file = new File("D:\\" + System.currentTimeMillis() + ".png");
        ChartUtilities.saveChartAsPNG(file, chart, 650, 500);
    }
}
